//index.js
//获取应用实例
const app = getApp()
const areaL = require("../../utils/area.js")
const Api = require("../../utils/api.js").API
const Store = require("../../utils/store.js").store
const addr = require('../../utils/address-parse.js')


Page({
    data: {
       SSQ: '',
         areaL: areaL.default,
         show:false,
        list: {
            "address": "",
            "applyRemark": "",
            "area": "",
            "city": "",
            "ckIdcard": "",
            "ckName": "",
            "fileInfoReqs": [
            	{
            		"attSize": 0,
            		"attUrl": "",
            		"busiType": "104102",
            		"sort": 0
            	},
                {
                	"attSize": 0,
                	"attUrl": "",
                	"busiType": "104101",
                	"sort": 0
                },
               
            ],
            "phoneNo": "",
            "province": "",
            "street": "",
            "wechatNo": "",
        }, 
        tipsShow: false,
        tipsText: '',
        titleText: '',
        userInfo:'',
         imglist:'',
         imglistS:'',
         fileList:[],
       
    },
   
    onLoad: function() {

       
    },
   onShow(){
       var token = Store.getStore('userInfo');
         var account = Store.getStore('myCustNo');
         
       
         if (token) { 
           this.setData({
           userInfo: token, 
           ['list.wechatNo']: account,
            ['list.phoneNo']: account
             
         }) 
           }
   },
   tips: function(e) {
       let num = e.currentTarget.dataset.num;
       let tips = [
           '获取方式：在微信中点击右下角打开“我”，点击你的头像，然后点击“二维码名片”即可。',
           '获取方式：在微信中点击右下角打开“我”，点击“支付”，在新页面点击“收付款”，然后点击“二维码收款”即可',
           '截取方式：在微信群中点击右上角”...“，通过手机截屏方式截取群信息，请在截屏前确保群人数大于50，且群聊名称为”好麦优选“。'
       ]
       let titles = [
           '获取方式',
           '获取方式',
           '截取方式'
       ]
       this.setData({
        tipsShow: true,
        tipsText: tips[num],
        titleText: titles[num]
       })

   },
   onClickHide: function() {
    this.setData({
        tipsShow: false
       })
   },
   clickJump: function() {
       this.setData({
           show: true
       })
   },
   cancelArea: function(e) {
       this.setData({
           show: false
       })
   },
   confirmArea: function(event) {
       var a = event.detail.values;
       console.log(a);
       var strArr = [];
       a.forEach(item => {
           strArr.push(item.name);
       })
       this.setData({
           SSQ: strArr.join(''),
           [`list.province`]: a[0].code,
           [`list.city`]: a[1].code,
           [`list.area`]: a[2].code,
           show: false
       })
   },
    afterRead(event) {
       const { file } = event.detail;
       console.log(file);
        const { fileList = [] } = this.data;
       
              
       
            Api.ZhuFuUploadApi(file.path,{}).then(resa=>{ 
                var resUrl = JSON.parse(resa);
                var a =  {
                  	"attSize": 0,
                  	"url": resUrl.url,
                  	"busiType": "104103",
                  	"sort": 0
                  }
                  fileList.push(a);
                  this.setData({ fileList });
                           
                // var a = {cheak:true,url:resUrl.url};
              
          
            
        })
        
     
       },
       deleteZ(event){
            var fileList = this.data.fileList;
           
            fileList.splice(event.detail.index,1);
              this.setData({ fileList });
       },
    onChangeName(event) {
        // event.detail 为当前输入的值
        console.log(event.detail);
        this.setData({

            [`list.ckName`]: event.detail,
        });
    },
    onChangePhone(event) {
        // event.detail 为当前输入的值
        console.log(event.detail);
        this.setData({

            [`list.phoneNo`]: event.detail,
        });
    },
    completemessage: function(){
        wx.showToast({
          title: '添加成功，管家会尽快与您联系',
          icon: 'none'
        })
    },
    onChangeStreet(event) {
        // event.detail 为当前输入的值
        console.log(event.detail);
        this.setData({

            [`list.ckIdcard`]: event.detail,
        });
    },
    onChangeStreet2(event) {
        // event.detail 为当前输入的值
        console.log(event.detail);
        this.setData({
    
            [`list.wechatNo`]: event.detail,
        });
    },
    jumpOrder: function() {
        var _this = this;
            wx.chooseImage({
                count: 1, // 默认9
                sizeType: ['original', 'compressed'],
                sourceType: ['album', 'camera'], //
                success: function(res) {
                    
                    var tempFilePaths = res.tempFilePaths;
                    // let a = 'list.fileInfoReqs[0].attUrl';
                     let a = `list.fileInfoReqs[0].attUrl`;
                    // _this.setData({
                    //             [a]: tempFilePaths[0],
                                                   
                    //         })
                         console.log(tempFilePaths);
                    tempFilePaths.forEach((item,i)=>{
                        Api.ZhuFuUploadApi(item,{}).then(resa=>{ 
                             var resUrl = JSON.parse(resa);
                           
                            // var a = {cheak:true,url:resUrl.url};
                            _this.setData({
                                [a]: resUrl.url,
             
                            })
                            console.log(_this.data.list);
                        });
                        
                    })              
                }
            })
    },
   
   jumpOrderS: function() {
       var _this = this;
           wx.chooseImage({
               count: 1, // 默认9
               sizeType: ['original', 'compressed'],
               sourceType: ['album', 'camera'], //
               success: function(res) {
                   var tempFilePaths = res.tempFilePaths
                      let a = `list.fileInfoReqs[1].attUrl`;
                      
                      tempFilePaths.forEach((item,i)=>{
                          Api.ZhuFuUploadApi(item,{}).then(resa=>{ 
                              var resUrl = JSON.parse(resa);
                              // var a = {cheak:true,url:resUrl.url};
                              _this.setData({
                                  [a]: resUrl.url,
                                   
                              })
                          });
                          
                      })
                   // _this.setData({
                   //             [a]: tempFilePaths[0],
                                                  
                   //         })
                        
               }
           })
   },
      
    jumpSave:function(){
      
            if(this.data.imglist.length == 0){
                return  wx.showToast({ title: "请选择图片保存上传！", icon: 'none' });
            }
            
            let data = 
                {
                	"blContent": "",
                	"blFile": this.data.cheakList[0].url,
                	"blTitle": this.data.ImageValue,
                	"blType": 180102,
                	"id": ""
                }
            
            let ckNo = wx.getStorageSync('ckNo');
            
           Api.zhufuSaveApi(data).then(resa=>{
               if(ckNo){
                   Api.getCkInfoApi(ckNo).then(res=>{
                       if(res.data == null){
                          
                       }else{
                            wx.setStorageSync('baseInfo',res.data);
                       }
                        wx.navigateBack()
                       
                        
                       
                   })
                 }else{
                     wx.navigateBack()
                 }
             
              
               
           })
      
    },
    
    
    
 
    backList: function() {
        
        
        var data = this.data.list;
        var a = [this.data.list.city,this.data.list.ckName]
     
      for (var i in a) {

          if (a[i] == "" ) {
             
              wx.showToast({
                 title: '信息未填写完整!',
                  icon: 'none',
                  duration: 3000
              })
      
              setTimeout(function() {
                  wx.hideToast()
              }, 3000)
              return
          }
      
      }
      var a = this.data.list.fileInfoReqs;
      var b = this.data.fileList;
      if(b.length == 0){
          return  wx.showToast({
               title: '请上传群截图!',
                icon: 'none',
               
            })
      }
      b.forEach(item=>{
        
          item.attUrl= item.url;
            
      })
      b.forEach(item=>{
         delete item.url;
      })
      
    var c =  a.concat(b);
    try {
      
        
        // 执行到第4次，结束循环
        c.forEach(function(item,index){
            if (item.attUrl == '') {
                throw new Error("EndIterative");
            }
            
        });
    } catch(e) {
        if(e.message!="EndIterative"){
            throw e;
        }else{
         return   wx.showToast({
               title: '请上传图片!',
                icon: 'none',
               
            })
        } 
    };
    var d = 'list.fileInfoReqs';
    this.setData({
        [d]:c
    })
    
      // console.log(c);
      
      
    
      

       
         // if (this.data.id == "") {
            Api.applyTuanApi(this.data.list).then(res => {
                // console.log(res);
                if (res.code === 0) {
                    wx.showToast({
                      title: '你的申请已经收到，我们会尽快为你审核。',
                      icon: 'none',
                      duration: 1500,
                      success: function() {
                        
                        setTimeout(function(){
                            wx.navigateBack();
                        }, 1500)
                      }
                    })
                }
                
            })
        // }else {
        //     data['id'] = this.data.id;
        //     Api.updateInfoApi(data).then(res => {
        //         console.log(res);

        //         wx.navigateBack();
        //     })
        // }


    },


});
